# -*- coding:utf-8 -*-
import datetime
from django.shortcuts import redirect
from django.utils.deprecation import MiddlewareMixin
from django.conf import settings

from .. import models


class Tracer(object):

    def __init__(self):
        self.user = None


class AuthMiddleware(MiddlewareMixin):

    def process_request(self, request):
        """ 如果用户已登录，则request中赋值 """

        request.tracer = Tracer()

        user_id = request.session.get('user_info', 0)
        user_object = models.User.objects.filter(id=user_id).first()
        if request.path_info in settings.WHITE_REGEX_URL_LIST:
            return
        request.tracer.user = user_object
        if not user_object:
            return redirect('login')
        
        return
    
